package com.cisystem.web.controller.business;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.cisystem.common.core.domain.entity.SysDept;
import com.cisystem.common.utils.poi.ExcelUtil;
import com.cisystem.system.domain.FileVerb;
import com.cisystem.system.service.IFileVerbService;
import com.cisystem.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cisystem.common.annotation.Log;
import com.cisystem.common.core.controller.BaseController;
import com.cisystem.common.core.domain.AjaxResult;
import com.cisystem.common.enums.BusinessType;
import com.cisystem.common.core.page.TableDataInfo;

/**
 * 文件浏览Controller
 *
 * @author xxx
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/business/verb")
public class FileVerbController extends BaseController {
    @Autowired
    private IFileVerbService fileVerbService;

    @Autowired
    ISysDeptService deptService;

    /**
     * 查询文件浏览列表
     */
    @GetMapping("/listVerb")
    @PreAuthorize("@ss.hasPermi('business:verb:list')")
    public TableDataInfo listVerb(FileVerb fileVerb) {
        startPage();
        List<FileVerb> list = fileVerbService.selectFileVerbListGroup(fileVerb);
        TableDataInfo dataTable = getDataTable(list);
        List<?> rows = dataTable.getRows();
        List<SysDept> sysDepts = deptService.selectDeptList(new SysDept());
        Map<Long,SysDept> sysDeptMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(sysDepts)) {
            sysDeptMap = sysDepts.stream().collect(Collectors.toMap(SysDept::getDeptId, Function.identity(), (k1, k2) -> k1));
        }

        if (rows != null && !rows.isEmpty()) {
            for (Object row : rows) {
                FileVerb fileVerb1 = (FileVerb) row;
                if (fileVerb1.getDeptId() != null) {
                    SysDept sysDept = sysDeptMap.get(Long.valueOf(fileVerb1.getDeptId()));
                    if (sysDept != null) {
                        fileVerb1.setDeptName(sysDept.getDeptName());
                    }
                }
            }
        }
        return dataTable;
    }
    /**
     * 查询文件浏览列表
     */
    @PreAuthorize("@ss.hasPermi('business:verb:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(FileVerb fileVerb) {
        return success(fileVerbService.selectFileVerbList(fileVerb));
    }

    /**
     * 导出文件浏览列表
     */
    @PreAuthorize("@ss.hasPermi('business:verb:export')")
    @Log(title = "文件浏览", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FileVerb fileVerb) {
        List<FileVerb> list = fileVerbService.selectFileVerbList(fileVerb);
        ExcelUtil<FileVerb> util = new ExcelUtil<FileVerb>(FileVerb.class);
        util.exportExcel(response, list, "文件浏览数据");
    }

    /**
     * 获取文件浏览详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:verb:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fileVerbService.selectFileVerbById(id));
    }

    /**
     * 新增文件浏览
     */
    @Log(title = "文件浏览", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileVerb fileVerb) {
        try {
            String fileName = fileVerb.getFileName();
            fileVerb.setFilePath(fileName);
            String[] split = fileName.split("\\\\");
            String s = split[split.length - 1];
            fileVerb.setFileName(s);
            fileVerb.setReadTime(new Date());
            return toAjax(fileVerbService.insertFileVerb(fileVerb));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    /**
     * 修改文件浏览
     */
    @Log(title = "文件浏览", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileVerb fileVerb) {
        fileVerb.setReadTime(new Date());
        return toAjax(fileVerbService.updateFileVerb(fileVerb));
    }
    /**
     * 修改文件浏览
     */
    @Log(title = "文件浏览", businessType = BusinessType.UPDATE)
    @PutMapping("/editStatus")
    public AjaxResult editStatus(@RequestBody FileVerb fileVerb) {
        fileVerb.setReadTime(new Date());
        return toAjax(fileVerbService.updateFileVerbStatus(fileVerb));
    }

    /**
     * 删除文件浏览
     */
    @Log(title = "文件浏览", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fileVerbService.deleteFileVerbByIds(ids));
    }
}
